import { defineStore } from 'pinia'
import { getDevDoc, getDoc } from './api'


export const useStore = defineStore('Common', {
    state: () => {
        return {
            tree: {
                data: [],
                Folders: {},
                Items: {},
                active: null,
                open: [],
            },
            search: {
                key: null,
                list: [],
            },
            tabs: {
                list: [],
            },
            IP: null,
            headers: {},
        }
    },
    actions: {
        getItemData(item) {
            var _data = {};
            if (this.tree.Items[item.id]) _data.item = this.tree.Items[item.id];
            if (this.tree.Folders[item.parent]) _data.folder = this.tree.Folders[item.parent];
            return _data;
        },
        setTreeActive(item) {
            this.tree.active = item.id;
            let isHave = false;
            for (let i = 0; i < this.tabs.list.length; i++) {
                const element = this.tabs.list[i];
                if (element.id == item.id) {
                    isHave = true;
                    break
                }
            };
            if (!isHave) {
                this.tabs.list.push({
                    title: item.title,
                    id: item.id,
                    type: item.type,
                    parent: item.parent,
                    url: item.url,
                    parent: item.parent
                });
            }
        },
        removeTabsItem(targetKey, type) {
            let lastIndex = 0;
            this.tabs.list.forEach((pane, i) => { if (pane.id === targetKey) lastIndex = i - 1 });
            if (type == 'self') {
                this.tabs.list = this.tabs.list.filter(pane => pane.id !== targetKey);
                if (this.tabs.list.length && this.tree.active === targetKey) {
                    if (lastIndex >= 0) {
                        this.tree.active = this.tabs.list[lastIndex].id
                    } else {
                        this.tree.active = this.tabs.list[0].id
                    }
                };
            } else if (type == 'left') {
                this.tabs.list = this.tabs.list.slice(lastIndex + 1);
                this.tree.active = targetKey;
            } else if (type == 'right') {
                this.tabs.list = this.tabs.list.slice(0, lastIndex + 2);
                this.tree.active = targetKey;
            } else if (type == 'other') {
                this.tabs.list = [this.tabs.list[lastIndex + 1]];
                this.tree.active = targetKey;
            } else if (type == 'all') {
                this.tabs.list = [];
            }
            if (!this.tabs.list.length) {
                this.tree.active = null;
            }
        },
        CloseAllTreeFolder() {
            this.tree.open = [];
        },
        async getData(url) {
            return getDoc(url).then(res => {
                this.tree.data = res.tree;
                this.tree.Folders = res.treeFolder;
                this.tree.Items = res.treeItem;
                this.IP = res._IP;
                this.tabs.list = [];
                this.tree.active = null;
                this.headers = {};
                return true;
            });
        }
    }
})